#include "InvaderController.h"

namespace game
{	
	InvaderController::InvaderController(int _row, int _collumn) 
		:	maxLeft(-15.0f), maxRight(15.0f), blockWSize(25.0f), 
			blockHSize(15.0f), top(25.0f), movingRight(true), delay(20), 
			curDelay(0), row(_row), collumn(_collumn), hasReseted(false), 
			orientationFlag(false),rotationModifier(1)
	{
		for(int c = 0; c < collumn; c++)
		{
			invaders.push_back(std::vector<sprites::Sprite*>());
			invadersDefault.push_back(std::vector<sprites::Sprite*>());
			for(int r = 0; r < row; r++)
			{
				invaders[c].push_back(new sprites::Invader(collisions::Vector3D(maxLeft + (blockWSize / collumn) * c, top - (blockHSize / row) * r, -5.0f), collisions::INVADER, 1.05f, 0.35f));
				invadersDefault[c].push_back(invaders[c][r]);
			}
		}
	};

	void InvaderController::draw()
	{
		//delete dead invaders from the list
		for(unsigned int i = 0; i < invaders.size();)
		{
			for(unsigned int j = 0; j < invaders[i].size();)
			{
				if(invaders[i][j]->isDead())
				{
					invaders[i].erase(invaders[i].begin()+j);
				}
				else
				{
					j++;
				}
			}
			//delete empty vectors from the list
			if(invaders[i].size() == 0)
			{
				invaders.erase(invaders.begin() + i);
			}
			else
			{
				i++;
			}
		}
		//If we have been signaled to rotate, do so now
		if(orientationFlag){
			orientationFlag = false; //reset the orientation flag back to false
			rotationModifier *= -1.0f; //change the modifier for our rotation
			for(unsigned int i = 0; i < invaders.size(); i++)
			{
				for(unsigned int j = 0; j < invaders[i].size(); j++)
				{
					invaders[i][j]->rotateForward(rotationModifier*-90.0f);
				}
			}
		}

		if(invaders.size() == 0)
		{
			hasReseted = true;
			reset();
			return;
		}
	
		//determine if we must move down
		if(curDelay == delay)
		{
			curDelay = 0;

			if(movingRight)
			{
				if(invaders[invaders.size()-1][0]->getPosition().X >= maxRight)
				{		
					movingRight = false;
					for(unsigned int i = 0; i < invaders.size(); i++)
					{
						for(unsigned int j = 0; j < invaders[i].size(); j++)
						{
							((sprites::Invader*)invaders[i][j])->moveDown();
						}
					}
				}
				else
				{	
					for(unsigned int i = 0; i < invaders.size(); i++)
					{
						for(unsigned int j = 0; j < invaders[i].size(); j++)
						{
							((sprites::Invader*)invaders[i][j])->moveRight();
						}
					}
				}
			}
			else
			{
				if(invaders[0][0]->getPosition().X <= maxLeft)
				{		
					if(delay >= 2)
					{
						delay--;
					}
					movingRight = true;
					for(unsigned int i = 0; i < invaders.size(); i++)
					{
						for(unsigned int j = 0; j < invaders[i].size(); j++)
						{
							((sprites::Invader*)invaders[i][j])->moveDown();
						}
					}
				}
				else
				{	
					for(unsigned int i = 0; i < invaders.size(); i++)
					{
						for(unsigned int j = 0; j < invaders[i].size(); j++)
						{
							((sprites::Invader*)invaders[i][j])->moveLeft();
						}
					}
				}
			}
		}
		else
		{
			curDelay++;
		}
		for(unsigned int i = 0; i < invaders.size(); i++)
		{
			for(unsigned int j = 0; j < invaders[i].size(); j++)
			{
				if(rand() % 500 == 0)
				{
					if(!((sprites::Invader*)invaders[i][j])->isShooting())
						bullets.push_back(((sprites::Invader*)invaders[i][j])->shoot());
				}
			}
		}
		/*
		for(unsigned int i = 0; i < invaders.size(); i++)
		{
			for(unsigned int j = 0; j < invaders[i].size(); j++)
			{
				invaders[i][j]->draw();
			}
		}*/
	}
	vector<sprites::Sprite*> InvaderController::getInvaders()
	{//passes a list of the invaders
		hasReseted = false;
		vector<sprites::Sprite*> jad = vector<sprites::Sprite*>();
		for(unsigned int i = 0; i < invaders.size(); i++)
		{
			for(unsigned int j = 0; j < invaders[i].size(); j++)
			{
				jad.push_back(invaders[i][j]);
			}
		}
		return jad;
	}

	void InvaderController::reset()
	{
		if(!hasReseted)
		{//player died, reset the game completly
			delay = 20;
		}
		hasReseted = true;
		invaders = invadersDefault;

		for(int c = 0; c < collumn; c++)
		{
			invaders.push_back(std::vector<sprites::Sprite*>());
			invadersDefault.push_back(std::vector<sprites::Sprite*>());

			for(int r = 0; r < row; r++)
			{
				invaders[c][r]->setPosition(collisions::Vector3D(maxLeft + (blockWSize / collumn) * c, top - (blockHSize / row) * r, -5.0f));
				invaders[c][r]->resetRotation();
				if(rotationModifier == -1)
				{
					invaders[c][r]->rotateForward(90.0f);
				}
				invaders[c][r]->revive();
			}
		}
	}

	bool InvaderController::hasReset()
	{
		return hasReseted;
	}

	vector<sprites::Sprite*> InvaderController::getBullets()
	{//passes a list of newly fired bullets
		vector<sprites::Sprite*> jad = vector<sprites::Sprite*>();
		for(unsigned int i = 0; i < bullets.size(); i++)
		{
			jad.push_back(bullets[i]);
		}
		bullets = vector<sprites::Sprite*>();
		return jad;
	}
}